package com.example.demo_01.service.impl;

import com.example.demo_01.mapper.TeaMapper;
import com.example.demo_01.service.TeaService;
import com.example.demo_01.util.ResultBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

@Service
public class TeaServiceImpl implements TeaService {
    @Autowired
    private TeaMapper teaMapper;

    @Override
    public ResultBean findByParam(Map<String, String> param) {
        int pageNum = Integer.parseInt(param.get("pageNum"));
        int pageSize = Integer.parseInt(param.get("pageSize"));
        PageHelper.startPage(pageNum , pageSize);
        List<Map> list = teaMapper.findByParam(param);
        if (list != null && list.size() > 0) {
            PageInfo<Map> pageInfo = new PageInfo<>(list);
            return ResultBean.getInstance().success().setData(pageInfo);
        }
        return ResultBean.getInstance().fail("查无数据");
    }

    @Override
    public ResultBean doSave(Map<String, String> param) {
        int row = teaMapper.save(param);
        if (row > 0) {
            return ResultBean.getInstance().success("保存成功");
        }
        return ResultBean.getInstance().fail("保存失败");
    }

    @Override
    public ResultBean doUpdate(Map<String, String> param) {
        int row = teaMapper.update(param);
        if (row > 0) {
            return ResultBean.getInstance().success("更新成功");
        }
        return ResultBean.getInstance().fail("更新失败");
    }

    @Override
    public ResultBean delete(String ids) {
        int row = teaMapper.delete(ids);
        if (row > 0) {
            return ResultBean.getInstance().success("删除成功");
        }
        return ResultBean.getInstance().fail("删除失败");
    }

    @Override
    public ResultBean getAnalysisData() {//统计年龄和薪资比例
        Map<String, Object> analysisData = new HashMap<>();
        List<Map<String, Object>> ageDistribution = teaMapper.getAgeDistribution();
        analysisData.put("ageDistribution", ageDistribution);
        List<Map<String, Object>> salaryDistribution = teaMapper.getSalaryDistribution();
        analysisData.put("salaryDistribution", salaryDistribution);
        return ResultBean.getInstance().success().setData(analysisData);
    }

    @Override
    public ResultBean getAgeSalaryDistribution() {
        List<Map<String, Object>> ageSalaryDistribution = teaMapper.getAgeSalaryDistribution();
        return ResultBean.getInstance().success().setData(ageSalaryDistribution);
    }

}
